package com.abbas.abchat.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.abbas.abchat.R;
import com.abbas.abchat.entity.Account;
import com.abbas.abchat.entity.ChatTable;
import com.abbas.abchat.utils.TimeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.List;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/4/14 10:56
 */
public class ChatAdapter extends BaseAdapter {

    private Context mContext;
    private List<Account> accounts;
    private List<ChatTable> chatTables;

    public ChatAdapter(Context mContext, List<Account> accounts,List<ChatTable> chatTables) {
        this.mContext = mContext;
        this.accounts = accounts;
    }

    public void setAccounts(List<Account> accounts) {
        this.accounts = accounts;
    }

    public void setChatTables(List<ChatTable> chatTables) {
        this.chatTables = chatTables;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return accounts == null ? 0 : accounts.size();
    }

    @Override
    public Object getItem(int position) {
        return accounts.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.chat_item,null,false);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.time = (TextView) convertView.findViewById(R.id.time);
            viewHolder.lastContent = (TextView) convertView.findViewById(R.id.last_content);
            convertView.setTag(viewHolder);
        }else {
             viewHolder = (ViewHolder) convertView.getTag();
        }
        Account account = accounts.get(position);
        /**
         * java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
         *         at com.abbas.abchat.adapter.ChatAdapter.getView(ChatAdapter.java:75)
         */
        ChatTable chatTable = chatTables.get(position);

        if (TextUtils.isEmpty(account.getImgPath())){
            Glide.with(mContext)
                    .load(R.mipmap.ic_launcher)
                    .transform(new RoundedCorners(16))
                    .into(viewHolder.img);
        }else {
            Glide.with(mContext)
                    .load(account.getImgPath())
                    .transform(new RoundedCorners(16))
                    .into(viewHolder.img);
        }
        if(!TextUtils.isEmpty(account.getName())){
            viewHolder.name.setText(account.getName());
        }else {
            viewHolder.name.setText(account.getAccount());
        }

        if (chatTable != null){
            //TODO: 还需要时间类进行转换
            viewHolder.time.setText(TimeUtils.getInstance(chatTable.getLastTime()).compareNow());

            viewHolder.lastContent.setText(chatTable.getLastContent());
        }


        return convertView;
    }

    static class ViewHolder{
        ImageView img;
        TextView name;
        TextView time;
        TextView lastContent;
    }
}
